﻿using System;
using System.Xml;
using System.Net;
using System.Windows.Forms;
using SdmxMl.Manager;
using SdmxMl.Common;

namespace SAE
{
    /// <summary> Dialog Box for Loading SDMX-ML document via URI </summary>
    public partial class DlgUri : Form
    {
        #region Internal members

        private TreeSdmxManager _tm;

        #endregion

        #region C'tor

        public DlgUri(TreeSdmxManager tm)
        {
            InitializeComponent();
            _tm = tm;
            buttonLoad.Enabled = false;
        }
        #endregion

        #region Properties

        public XmlDocument XDoc { get; set; }

        #endregion

        #region Events

        private void textBoxUri_TextChanged(object sender, EventArgs e)
        {
            buttonLoad.Enabled = textBoxUri.Text.Length > 0;
        }

        private void buttonLoad_Click(object sender, EventArgs e)
        {
            try
            {
                string uriText = textBoxUri.Text.Trim();
                if (uriText.StartsWith(UrnManager.UrnBase))
                {
                    UrnParser p = new UrnParser(uriText);
                    if (p.IsSupported)
                    {
                        ArtefactRefTyped art = new ArtefactRefTyped(p.ArType, p.ArtefactId, p.AgencyId, p.Version);

                        // If already in memory, navigate otherwhile get from regidtry
                        if (NavigationManager.LocatenavigateProject(art.Type, art.FullIdent, p) == null)
                            NavigationManager.NavigateWithRegistry(art.Type, art.FullIdent, true, p);
                    }
                }
                else
                {

                    Uri uri = new Uri(textBoxUri.Text.Trim());
                    WebClient webclient = new WebClient();
                    // webclient.Credentials = new NetworkCredential("UA....

                    using (new SdmxMl.Helper.CWaitCursor())
                    {
                        XDoc = new XmlDocument();
                        XDoc.Load(webclient.OpenRead(uri));
                        _tm.LoadSdmxArtefact(null, true, XDoc);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Unable to read from URI. Reasion is: "+ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        private void textBoxUri_KeyDown(object sender, KeyEventArgs e)
        {
            if (buttonLoad.Enabled)
            {
                if ((e.KeyData & (Keys.Shift | Keys.Alt)) == 0)
                {
                    switch (e.KeyData & Keys.KeyCode)
                    {
                        case Keys.Return:
                            if ((e.KeyData & Keys.Control) == 0)
                            {
                                buttonLoad_Click(this, EventArgs.Empty);
                            }
                            break;
                    }
                }
            }
        }
        #endregion
    }
}
